package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwaiT;
import cn.e4j.modular.crm.entity.GwailT;
import cn.e4j.modular.crm.service.GwaiTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 属性特征分类维护管理
 *
 * @author shixc
 * @Date 2018/01/10
 */
@Controller
@RequestMapping("/crm/gwai260")
public class Gwai260Controller extends BaseController {

    @Autowired
    private GwaiTService gwaiTService;

    /**
     * 跳转产品分类设置页面
     */
    @RequiresPermissions("crm:gwai260:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai260.html";
    }


    /**
     * 查询产品分类信息列表
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAIENT", user.getDBC());
        List<Map<String, Object>> result = gwaiTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai260:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();

        //判断属性特征分类编号是否重复
        GwaiT gwait = this.gwaiTService.getByKey(inDto.getString("GWAI001"),user.getDBC());
        if (gwait != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWAI.getCode(),
                                                        CrmExceptionEnum.EXISTED_THE_GWAI.getMessage());
        }
        GwaiT gwai = new GwaiT();
        gwai.setGWAI001(inDto.getString("GWAI001"));
        gwai.setGWAI002(" ");
        gwai.setGWAI003(" ");
        gwai.setGWAIACTI(inDto.getString("GWAIACTI"));
        gwai.setGWAIENT(user.getDBC());
        gwai.setGWAIUSER(user.getACCOUNT());
        gwai.setGWAIGRUP(user.getORG_ID());
        Date date = new Date();
        gwai.setGWAITIME(DateUtils.formatDate(date, "HH:mm:ss"));
        LocalDate today = LocalDate.now();
        gwai.setGWAIDATE(today);
        gwai.setGWAIORIG(user.getORG_ID());
        gwai.setGWAIORIU(user.getACCOUNT());
        GwailT gwail = new GwailT();
        gwail.setGWAILENT(user.getDBC());
        gwail.setGWAIL001(gwai.getGWAI001());
        gwail.setGWAIL002(LangUtils.getLang(user.getLANG()));
        gwail.setGWAIL003(inDto.getString("GWAIL003"));
        gwail.setGWAIL004(inDto.getString("GWAIL004"));
        gwaiTService.add(gwai, gwail);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai260:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        //判断属性特征分类编号是否存在
        GwaiT gwai = this.gwaiTService.getByKey(inDto.getString("GWAI001"),user.getDBC());
        if (gwai == null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWAI.getCode(),
                                                        CrmExceptionEnum.EXISTED_THE_GWAI.getMessage());
        }
        inDto.put("GWAIENT", user.getDBC());
        inDto.put("GWAIMODU", user.getACCOUNT());
        gwaiTService.edit(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai260:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAIENT", user.getDBC());
        gwaiTService.del(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwai260:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwaiT> gwaiTList = inDto.getReqList(GwaiT.class);
        gwaiTService.deleteDataBatch(gwaiTList);
        return SUCCESS_TIP;
    }


}
